<?php

namespace YmPhp\Admin\Services;

use YmPhp\Admin\Repositories\ResponseCode;
use Illuminate\Support\Facades\Validator;
use Jiannei\Response\Laravel\Support\Facades\Response;

class ValidateServices extends BasicServices
{
    protected $rules;


    public function __construct()
    {
        $this->rules['mobile'] = ['required', 'numeric'];
        $this->rules['password'] = ['required'];
        $this->rules['type'] = ['numeric', 'min:0', 'max:6'];
        $this->rules['email'] = ['required', 'email:rfc'];
        $this->rules['name'] = ['required'];
    }

    /**
     * 验证规则
     * @param $data
     * @param $fields
     * @return mixed
     */
    public function check($data, $fields)
    {
        $rules = [];
        foreach ($fields as $field) {
            if (isset($this->rules[$field]) && !empty($this->rules[$field]))
                $rules[$field] = $this->rules[$field];
        }
        $validator = Validator::make($data, $rules);
        if ($validator->fails()) Response::fail($validator->errors()->first(), ResponseCode::VERIFY_ERROR);
        return $data;
    }
}
